package forms;

import helpers.UrlHelper;

import java.net.URL;
import java.util.TreeMap;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import entities.OwnerDomain;

public class OwnerDomainForm {
	public static TreeMap checkValidUrl(HttpServletRequest request) {
		TreeMap check = new TreeMap();
		String yoursite = (String) request.getParameter("yoursite");
		
		if (!Pattern.matches("^(http://)[a-zA-Z0-9-]+([.][a-zA-Z0-9-]+)+", yoursite)) {
			check.put("yoursite", "Your site is not a url valid");
		}

		return check;
	}
	
	public static TreeMap checkVerify(HttpServletRequest request, OwnerDomain ownerDomain) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String url = "";
		String verifyMethod = (String) request.getParameter("verifyMethod");
		if (verifyMethod == null) {
			check.put("verifyMethod", "Verify Method is not empty value");
		} else if (verifyMethod.equalsIgnoreCase("url")) {
			ownerDomain.setVerifyMethod(verifyMethod);
			url = "http://" + ownerDomain.getDomain() + "/" + ownerDomain.getCodeVerify() + ".txt";
			try {
				Document doc = Jsoup.connect(url).get();
				if (!doc.text().equals(ownerDomain.getCodeVerify())) {
					check.put("verifyUrl", "Not verify.");
				}
			} catch (Exception e) {
				check.put("verifyUrl", "Verify url error.");
			}
		} else {
			ownerDomain.setVerifyMethod(verifyMethod);
			url = "http://" + ownerDomain.getDomain();
			try {
				Document doc = Jsoup.connect(url).get();
				if (!doc.select("meta[name="+ UrlHelper.getServerUrl(request) +"/validation]").attr("content").equals(ownerDomain.getCodeVerify())) {
					check.put("verifyCode", "Not verify.");
				}
			} catch (Exception e) {
				check.put("verifyCode", "Verify code error.");
			}
			
		}
		
		form.put("checkValid", check);
		form.put("ownerDomain", ownerDomain);
		
		return form;
	}
	
	public static TreeMap checkValidTitle(HttpServletRequest request, OwnerDomain ownerDomain) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String title = (String) request.getParameter("title");
		
		if (title == null || title.isEmpty()) {
			check.put("title", "Title is not empty.");
		}
		ownerDomain.setTitle(title);
		
		form.put("checkValid", check);
		form.put("ownerDomain", ownerDomain);
		
		return form;
	}
	
	public static TreeMap checkValidDescription(HttpServletRequest request, OwnerDomain ownerDomain) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String description = (String) request.getParameter("description");
		
		if (description == null || description.isEmpty()) {
			check.put("description", "Description is not empty.");
		}
		ownerDomain.setDescription(description);
		
		form.put("checkValid", check);
		form.put("ownerDomain", ownerDomain);
		
		return form;
	}
	
	public static TreeMap checkValidActive(HttpServletRequest request, OwnerDomain ownerDomain) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String active = (String) request.getParameter("active");
		
		try {
			ownerDomain.setActive(Integer.parseInt(active));
		} catch (Exception e) {
			check.put("active", "Description is not empty.");
		}
		
		form.put("checkValid", check);
		form.put("ownerDomain", ownerDomain);
		
		return form;
	}
}
